package com.baibei.pay.password;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.IPresenter;
import com.baibei.basic.module.utils.SMSCountDownTimer;
import com.baibei.ebec.sdk.IPushApi;
import com.baibei.model.UserInfo;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;
import com.baibei.pay.R;
import com.baibei.pay.R2;
import com.baibei.pay.recharge.SimpleTextWatcher;
import com.baibei.ui.AppUI;
import com.baibei.widget.Toolbar;
import com.rae.swift.Rx;
import com.rae.swift.session.SessionManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.baibei.module.Constant.SETFUNDPWD_SUCCESS;

@Route(path = AppRouter.ROUTER_USER_RESET_PAYMENT_PASSWORD)
public class SetFundPwdActivity extends BasicActivity implements SetFundPwdContract.SetFundPwdView {

    @BindView(android.R.id.title)
    TextView mTvtitle;
    @BindView(R2.id.tv_phone)
    TextView mTvPhone;
    @BindView(R2.id.et_checkcode)
    EditText mEtCheckcode;
    @BindView(R2.id.et_set_pwd)
    EditText mEtSetPwd;
    @BindView(R2.id.tv_isshow)
    TextView mTvIsshow;
    @BindView(R2.id.tv_get_checkcode)
    TextView mTvGetCheckcode;
    @BindView(R2.id.btn_commit)
    TextView mBtnCommit;
    @BindView(R2.id.tv_sub_title)
    TextView mTvSubTitle;

    /**
     * 倒计时
     */
    private SMSCountDownTimer mSMSCountDownTimer = new SMSCountDownTimer();

    /**
     * 表现层
     */
    private SetFundPwdContract.Presenter mPresenter;

    /**
     * 设置密码
     */
    public static final int TYPE_SET = 0;

    /**
     * 重设密码
     */
    public static final int TYPE_RESET = 1;

    /**
     * 页面类型
     */
    private int mType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_fund_pwd);
        ButterKnife.bind(this);
        setNavigationIcon(R.drawable.ic_closed);
        mPresenter = inject(SetFundPwdContract.Presenter.class);
        initView();
    }

    private void initView() {
        mType = getIntent().getIntExtra("type", TYPE_SET);
        UserInfo userInfo = SessionManager.getDefault().getUser();
        if (userInfo == null) {
            return;
        }
        SimpleTextWatcher textWatcher = new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mBtnCommit.setEnabled(mEtSetPwd.getEditableText().length() == 6 && !TextUtils.isEmpty(mEtCheckcode.getEditableText().toString()));
            }
        };
        mEtSetPwd.addTextChangedListener(textWatcher);
        mEtCheckcode.addTextChangedListener(textWatcher);
        mEtSetPwd.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
        String phone = Rx.blockingPhone(userInfo.getMobile());

        //根据类型显示
        if (mType == TYPE_SET) {
            mTvPhone.setText(String.format(getString(R.string.send_phone_checkcode), phone));
            mTvSubTitle.setVisibility(View.VISIBLE);
        } else if (mType == TYPE_RESET) {
            mTvPhone.setText(String.format(getString(R.string.send_reset_phone_checkcode), phone));
            mTvSubTitle.setVisibility(View.INVISIBLE);
        }
        //子标题显示隐藏
        mTvSubTitle.setVisibility(mType == TYPE_SET ? View.VISIBLE : View.GONE);
        mTvGetCheckcode.setText("获取验证码");
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        final Toolbar toolbar = (Toolbar) mToolbar;
        toolbar.post(new Runnable() {
            @Override
            public void run() {
                if (mType == TYPE_SET) {
                    toolbar.setTitle(getString(R.string.set_fund_pwd));
                } else if (mType == TYPE_RESET) {
                    toolbar.setTitle(getString(R.string.reset_fund_pwd));
                }
            }
        });
    }

    @Override
    public IPresenter getBasicPresenter() {
        return mPresenter;
    }


    @OnClick(R2.id.btn_commit)
    public void commit() {
        //提交设置资金密码
        if (mType == TYPE_SET) {
            mPresenter.setFundPassword();
        }
        if (mType == TYPE_RESET) {
            mPresenter.resetFundPassword();
        }
    }

    @OnClick(R2.id.tv_isshow)
    public void isShowPwd() {
        int type = mEtSetPwd.getInputType();
        int resource = R.drawable.ic_eyes_closed;
        if (type == InputType.TYPE_CLASS_NUMBER) {
            //pwd
            mEtSetPwd.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
        } else {
            //显示
            mEtSetPwd.setInputType(InputType.TYPE_CLASS_NUMBER);
            resource = R.drawable.ic_eyes_open;
        }
        mTvIsshow.setCompoundDrawablesWithIntrinsicBounds(0, 0, resource, 0);
    }

    @OnClick(R2.id.tv_get_checkcode)
    public void onCheckCodeClickListener() {
        if (mSMSCountDownTimer.isFinish()) {
            mPresenter.getCheckCode();
            mSMSCountDownTimer.start(this);
        }
    }


    @Override
    protected void onDestroy() {
        if (mSMSCountDownTimer != null) {
            mSMSCountDownTimer.cancel();
        }
        super.onDestroy();
    }


    @Override
    public String getPhoneNum() {
        UserInfo user = SessionManager.getDefault().getUser();
        return user.getMobile();
    }

    @Override
    public String getCheckCodeType() {
        return IPushApi.SMS_TYPE_FUND_PASSWPRD;
    }

    @Override
    public String getPassword() {
        return mEtSetPwd.getEditableText().toString();
    }

    @Override
    public String getCheckCode() {
        return mEtCheckcode.getEditableText().toString();
    }

    @Override
    public void onSuccess() {
        if (mType == TYPE_SET) {
            toast(getString(R.string.set_pwd_success));
            setResult(SETFUNDPWD_SUCCESS);
        }
        if (mType == TYPE_RESET) {
            toast(getString(R.string.reset_pwd_success));
        }
        finish();
    }

    @Override
    public void onFailed(String msg) {
        toast(msg);
    }

    @Override
    public void showLoading() {
        AppUI.loading(this, "操作中");
    }

    @Override
    public void hideLoading() {
        AppUI.dismiss();
    }

    @Override
    public void stopCountDown() {
        //停止倒计时
        if (mSMSCountDownTimer != null) {
            mSMSCountDownTimer.finish();
            onSmsCountDownFinish();
        }
    }

    @Override
    public void onSmsSuccess() {
        AppUI.success(getContext(), "短信验证码发送成功");
    }

    @Override
    public void onSmsFailed(String s) {
        AppUI.failed(getContext(), s);
    }

    @Override
    public void onSmsCountDown(int i, String s) {
        mTvGetCheckcode.setText(s);
    }

    @Override
    public void onSmsCountDownFinish() {
        mTvGetCheckcode.setText(R.string.reload_checkcode);
        mSMSCountDownTimer.cancel();
    }
}
